package cn.iocoder.yudao.module.wine.controller.admin.supplyinfo;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplylabel.SupplyLabelDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplytype.SupplyTypeDO;
import cn.iocoder.yudao.module.wine.service.supplylabel.SupplyLabelService;
import cn.iocoder.yudao.module.wine.service.supplytype.SupplyTypeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplyinfo.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyinfo.SupplyInfoDO;
import cn.iocoder.yudao.module.wine.service.supplyinfo.SupplyInfoService;

@Tag(name = "管理后台 - 供求信息")
@RestController
@RequestMapping("/wine/supply-info")
@Validated
public class SupplyInfoController {

    @Resource
    private SupplyInfoService supplyInfoService;

    @Resource
    private SupplyLabelService labelService;

    @Resource
    private SupplyTypeService supplyTypeService;

    @Resource
    private DictDataApi dataApi;

    @PostMapping("/create")
    @Operation(summary = "创建供求信息")
    @PreAuthorize("@ss.hasPermission('wine:supply-info:create')")
    public CommonResult<Long> createSupplyInfo(@Valid @RequestBody SupplyInfoSaveReqVO createReqVO) {
        return success(supplyInfoService.createSupplyInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供求信息")
    @PreAuthorize("@ss.hasPermission('wine:supply-info:update')")
    public CommonResult<Boolean> updateSupplyInfo(@Valid @RequestBody SupplyInfoSaveReqVO updateReqVO) {
        supplyInfoService.updateSupplyInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供求信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-info:delete')")
    public CommonResult<Boolean> deleteSupplyInfo(@RequestParam("id") Long id) {
        supplyInfoService.deleteSupplyInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供求信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-info:query')")
    public CommonResult<SupplyInfoRespVO> getSupplyInfo(@RequestParam("id") Integer id) {
        SupplyInfoDO supplyInfo = supplyInfoService.getSupplyInfo(id);
        if (supplyInfo!=null) {

            List<DictDataRespDTO> unit = dataApi.getDictDataList("unit");
            List<DictDataRespDTO> packing = dataApi.getDictDataList("packing");
            List<DictDataRespDTO> year = dataApi.getDictDataList("year");

            SupplyInfoRespVO infoRespVO = BeanUtils.toBean(supplyInfo, SupplyInfoRespVO.class);
            String format = AreaUtils.format(infoRespVO.getAreaId(), " ");
            infoRespVO.setAreaName(format);
            unit.forEach(dict -> {
                if (dict.getValue().equals(String.valueOf(infoRespVO.getUnit()))) {
                    infoRespVO.setUnitName(dict.getLabel());
                }
            });
            packing.forEach(dict -> {
                if (dict.getValue().equals(String.valueOf(infoRespVO.getPacking()))) {
                    infoRespVO.setPackingName(dict.getLabel());
                }
            });
            year.forEach(dict -> {
                if (dict.getValue().equals(String.valueOf(infoRespVO.getShowYear()))) {
                    infoRespVO.setShowYearName(dict.getLabel());
                }
            });
            return success(infoRespVO);
        }
        return null;
    }

    @GetMapping("/page")
    @Operation(summary = "获得供求信息分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-info:query')")
    public CommonResult<PageResult<SupplyInfoRespVO>> getSupplyInfoPage(@Valid SupplyInfoPageReqVO pageReqVO) {
        PageResult<SupplyInfoRespVO> pageResult = supplyInfoService.getSupplyInfoPage(pageReqVO);

        List<DictDataRespDTO> unit = dataApi.getDictDataList("unit");
        List<DictDataRespDTO> packing = dataApi.getDictDataList("packing");
        List<DictDataRespDTO> year = dataApi.getDictDataList("year");

        if (pageResult!=null) {

            List<SupplyInfoRespVO> list = pageResult.getList();
            if (!CollectionUtil.isEmpty(list)) {

                List<SupplyInfoRespVO> collect = list.stream().peek(item -> {

                    String format = AreaUtils.format(item.getAreaId(), " ");
                    item.setAreaName(format);
                    unit.forEach(dict -> {

                        if (dict.getValue().equals(String.valueOf(item.getUnit()))) {
                            item.setUnitName(dict.getLabel());
                        }
                    });
                    packing.forEach(dict -> {
                        if (dict.getValue().equals(String.valueOf(item.getPacking()))) {
                            item.setPackingName(dict.getLabel());
                        }
                    });

                    year.forEach(dict -> {
                        if (dict.getValue().equals(String.valueOf(item.getShowYear()))) {
                            item.setShowYearName(dict.getLabel());
                        }
                    });
                }).collect(Collectors.toList());

                pageResult.setList(collect);
            }
        }

        return success(pageResult);
    }

    @GetMapping("/labelList")
    @Operation(summary = "获得特殊标签列表")
    @PreAuthorize("@ss.hasPermission('wine:supply-info:query')")
    public CommonResult<List<SupplyLabelDO>> getSupplyLabelList() {
        List<SupplyLabelDO> list = labelService.selectLabelList();
        return success(list);
    }

    @GetMapping("/typeList")
    @Operation(summary = "获得信息类型列表")
    @PreAuthorize("@ss.hasPermission('wine:supply-info:query')")
    public CommonResult<List<SupplyTypeDO>> getSupplyTypeList() {
        List<SupplyTypeDO> list = supplyTypeService.selectTypeList();
        return success(list);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出供求信息 Excel")
//    @PreAuthorize("@ss.hasPermission('wine:supply-info:export')")
//    @OperateLog(type = EXPORT)
//    public void exportSupplyInfoExcel(@Valid SupplyInfoPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<SupplyInfoDO> list = supplyInfoService.getSupplyInfoPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "供求信息.xls", "数据", SupplyInfoRespVO.class,
//                        BeanUtils.toBean(list, SupplyInfoRespVO.class));
//    }

}
